package com.atguigu.bigdata.spark.zzgcore.rdd.operator.transform

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
 * @Classname Spark01_RDD_Operation_Transfrom
 * @Description 相同的首字母放在一个组中
 * @Date 2023/9/20 15:23
 * @Author zhuzhenguo
 */
object Spark09_RDD_Operation_Transform {
  def main(args: Array[String]): Unit = {
    // 准备环境,这个 *表示系统当前最大可用核数
    val sparkConf: SparkConf = new SparkConf().setMaster("local[*]").setAppName("Operator")
    val sc = new SparkContext(sparkConf)
    val rdd: RDD[Int] = sc.makeRDD(List(1,2,3,4,1,2,3,4))
    // 分布式的方式实现去重
    // map(x => (x, null)).reduceByKey((x, _) => x, numPartitions).map(_._1)
    // (1,null),(2,null),(3,null),(4,null),(1,null),(2,null),(3,null),(4,null)
    // reduceByKey会把相同的key将value做聚合
    // (1,null),(1,null) => (null, null) => null => (1,null)
    val rdd1: RDD[Int] = rdd.distinct()
    rdd1.collect().foreach(println)

    // 关闭环境
    sc.stop()
  }
}
